/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import model.LoadFromCSV;
import model.WriteToCSV;

/**
 * Menu bar for MainWindow
 * @author Sacha MASSON
 */
public class Menu extends JMenuBar implements ActionListener{

    JMenu file;
    JMenuItem open;
    JMenuItem save;
    JMenuItem exit;

    JMenu contact;
    JMenuItem add;

   
    ArrayList a;

    /**
     * Constructor
     * @param al ArrayList of contact
     * @param m MainWindow
     */
    public Menu(ArrayList al, MainFrame m) {
        super();
        a=al;
        file=new JMenu("File");
        exit=new JMenuItem("Exit");
        open=new JMenuItem("Open");
        save=new JMenuItem("Save");
        open.addActionListener(this);
        save.addActionListener(this);
        exit.addActionListener(this);
        file.add(open);
        file.add(save);
        file.add(exit);

        contact=new JMenu("Contacts");
        add=new JMenuItem("Add a contact");
        add.addActionListener(this);
        contact.add(add);

        this.add(file);
        this.add(contact);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==exit) System.exit(0);
        if(e.getSource()==add) new FormAdd(a);
        if(e.getSource()==open) {
            LoadFromCSV.load();
            JOptionPane.showMessageDialog(file,"Contact successfully loaded");
        }
        if(e.getSource()==save) {
            WriteToCSV.csvwrite(a);
              JOptionPane.showMessageDialog(file,"Contact successfully saved");
        }
    }

}